package com.xindacloud.sms.adapter.service.business.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xindacloud.sms.adapter.dao.SmsDownloadReportLogMapper;
import com.xindacloud.sms.adapter.dto.SmsReportDTO;
import com.xindacloud.sms.adapter.entity.SmsDownloadReportLog;
import com.xindacloud.sms.adapter.entity.SmsUser;
import com.xindacloud.sms.adapter.service.CommonServiceImpl;
import com.xindacloud.sms.adapter.service.business.ISmsDownloadReportLogService;
import com.xindacloud.sms.adapter.utils.DateHelp;
import com.xindacloud.sms.adapter.utils.StatusEum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Objects;


@Slf4j
@Service
public class SmsDownloadReportLogServiceImpl extends ServiceImpl<SmsDownloadReportLogMapper, SmsDownloadReportLog> implements ISmsDownloadReportLogService {

    @Autowired
    CommonServiceImpl commonService;

    @Override
    public SmsDownloadReportLog saveSmsDownloadReportLog(SmsReportDTO smsReportDTO, String username, String password) {

        final SmsUser smsUser = commonService.getSmsUser(username, password);
        if (Objects.isNull(smsUser)) {
            return null;
        }
        final SmsDownloadReportLog downloadReportLog = SmsDownloadReportLog.builder()
                .actionType(smsReportDTO.getActionType())
                .requestType(smsReportDTO.getRequestType())
                .queryReportStartTime(Objects.nonNull(smsReportDTO.getQueryReportStartTime())
                        ? DateHelp.str2LocalDateTimePlusHours(smsReportDTO.getQueryReportStartTime(), DateHelp.Y_M_D_H_M_S_PATTERN, 0)
                        : LocalDateTime.now())
                .queryReportEndTime(Objects.nonNull(smsReportDTO.getQueryReportEndTime())
                        ? DateHelp.str2LocalDateTimePlusHours(smsReportDTO.getQueryReportEndTime(), DateHelp.Y_M_D_H_M_S_PATTERN, 0)
                        : LocalDateTime.now())
                .downloadStatus(StatusEum.PENDING.desc)
                .userId(smsUser.getUserId())
                .build();
        save(downloadReportLog);
        return downloadReportLog;
    }

    @Override
    public boolean updateSmsDownloadReportLog(SmsDownloadReportLog smsDownloadReportLog) {
        return lambdaUpdate()
                .set(SmsDownloadReportLog::getDownloadStatus, smsDownloadReportLog.getDownloadStatus())
                .set(SmsDownloadReportLog::getDownloadReportFinishTime, LocalDateTime.now())
                .eq(SmsDownloadReportLog::getLogId, smsDownloadReportLog.getLogId())
                .update();
    }

    @Override
    public List<SmsDownloadReportLog> querySmsDownloadReportLog(String username, String password) {

        final SmsUser smsUser = commonService.getSmsUser(username, password);
        if (Objects.isNull(smsUser)) {
            return Collections.emptyList();
        }
        return lambdaQuery()
                .in(SmsDownloadReportLog::getUserId, commonService.queryUserIdsFor(smsUser.getUserId()))
                .orderByDesc(SmsDownloadReportLog::getCreateTime)
                .list();
    }
}
